package com.heeb.djl.boot.util;

/**
 * @Author bjiang
 * @Description //TODO
 * @Date 2022/4/7 14:51
 * @Version 1.0
 */

import ai.djl.modality.cv.Image;
import ai.djl.modality.cv.ImageFactory;
import org.bytedeco.javacpp.BytePointer;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.bytedeco.javacv.Java2DFrameUtils;
import org.bytedeco.javacv.OpenCVFrameConverter;
import org.bytedeco.opencv.global.opencv_core;
import org.bytedeco.opencv.global.opencv_highgui;
import org.bytedeco.opencv.global.opencv_imgproc;
import org.bytedeco.opencv.opencv_core.Mat;
import org.bytedeco.opencv.opencv_core.MatVector;
import org.bytedeco.opencv.opencv_core.Point;
import org.bytedeco.opencv.opencv_core.Scalar;
import org.opencv.imgproc.Imgproc;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Arrays;


/**
 * @Author hexin210426
 * @Description
 * @Date 2021/8/13
 **/
public class DrawRect {
    public static void main(String[] args) throws IOException {
        File file=new File("D:/work/kana1.jpg");
        //Image img = ImageFactory.getInstance().fromFile(imageFile);
        BufferedImage bufImg= ImageIO.read(file);
        Image img = ImageFactory.getInstance().fromImage(bufImg);
        Mat mat=bufferedImage2Mat(bufImg);
        opencv_highgui.imshow("123",mat);
        opencv_highgui.waitKey();
    }
    public static Mat bufferedImage2Mat(BufferedImage original) {
        OpenCVFrameConverter.ToMat cv = new OpenCVFrameConverter.ToMat();
        return cv.convertToMat(new Java2DFrameConverter().convert(original));
    }
    public static int[] getRGB(Mat mat, int row, int clos) {
        //获取像素点
        BytePointer ptr = mat.ptr(row, clos);
        int channels = mat.channels();

        int[] rgb = new int[3];//注意这里是BGR不是RGB
        for (int i = 0; i < channels; i++) {
            //根据通道获取BGR
            byte pixel = ptr.get(i);
            rgb[i] = pixel;
        }
        return rgb;
    }
}
